<?php
/**
 * Created by PhpStorm.
 * User: Zhanghaiqiang
 * Date: 2020-03-08
 * Time: 22:47
 */

namespace App\Core\Endpoints\CandyStory;


use Carbon\Carbon;
use Endpoints\Base\Endpoint;
use Endpoints\Tools\UploadOssObject;
use Models\Candy\Story;
use Models\Candy\StoryImages;
use Symfony\Component\Console\Input\InputArgument;

class CreateStory extends Endpoint
{
// 定义参数
    const ARGUMENT_TITLE    = Story::DB_FIELD_TITLE;
    const ARGUMENT_CONTENT  = Story::DB_FIELD_CONTENT;
    const ARGUMENT_DATE     = Story::DB_FIELD_DATE;
    const ARGUMENT_TYPE     = Story::DB_FIELD_STORY_TYPE;
    const ARGUMENT_MEDIA_CONTENTS = "media_contents";


    // 收参
    public function getArguments()
    {
        return [
            [static::ARGUMENT_TITLE,InputArgument::REQUIRED,'标题',''],
            [static::ARGUMENT_CONTENT,InputArgument::OPTIONAL,'内容',1],
            [static::ARGUMENT_DATE,InputArgument::REQUIRED,'日期',''],
            [static::ARGUMENT_TYPE,InputArgument::REQUIRED,'类型',1],
            [static::ARGUMENT_MEDIA_CONTENTS,InputArgument::OPTIONAL,'媒体',''],
        ];
    }

    /**
     * @return \Laravelladder\Core\Models\BaseModel|mixed
     * @throws \Laravelladder\Core\Exceptions\Endpoints\ArgumentRequiredException
     * @throws \Laravelladder\Core\Exceptions\Validations\ValidationException
     */
    public function dryRun()
    {
        $title      = $this->argument(static::ARGUMENT_TITLE);
        $content    = $this->argument(static::ARGUMENT_CONTENT);
        $saveDate   = $this->argument(static::ARGUMENT_DATE);
        $type       = $this->argument(static::ARGUMENT_TYPE);
        $mediaContents  = $this->argumentArray(static::ARGUMENT_MEDIA_CONTENTS);


        if($type == Story::STORY_TYPE_VALUE_IMAGE){
            $mediaSource = Story::MEDIA_SOURCE_ALIYUN;
            $mediaContent = $mediaContents[0];
            return $this->insertStory($title,$content,$saveDate,$type,$mediaSource,$mediaContent);
        }elseif($type == Story::STORY_TYPE_VALUE_VIDEO){
            $mediaSource = Story::MEDIA_SOURCE_URL;
            $mediaContent = $mediaContents[0];
            return $this->insertStory($title,$content,$saveDate,$type,$mediaSource,$mediaContent);
        }else{
            $mediaSource = Story::MEDIA_SOURCE_NONE;
            $storyModel = $this->insertStory($title,$content,$saveDate,$type,$mediaSource,'');
            foreach ($mediaContents as $index => $fileId){
                $this->getCandyStoryImagesRepo()->insertAndGetModel([
                    StoryImages::DB_FIELD_STORY_ID => $storyModel->getKey(),
                    StoryImages::DB_FIELD_SORT => $index,
                    StoryImages::DB_FIELD_IMAGE_SOURCE => StoryImages::IMAGE_SOURCE_ALIYUN,
                    StoryImages::DB_FIELD_IMAGE_VALUE => $fileId,
                ]);
            }
            return $storyModel;
        }
    }

    /**
     * @param $title
     * @param $content
     * @param $date
     * @param $type
     * @param $mediaSource
     * @param $mediaContent
     * @return \Laravelladder\Core\Models\BaseModel
     */
    public function insertStory($title,$content,$date,$type,$mediaSource,$mediaContent){
        $storyModel = [
            Story::DB_FIELD_TITLE => $title,
            Story::DB_FIELD_CONTENT => $content,
            Story::DB_FIELD_DATE => $date,
            Story::DB_FIELD_STORY_TYPE =>$type,
            Story::DB_FIELD_MEDIA_SOURCE => $mediaSource,
            Story::DB_FIELD_MEDIA_CONTENT => $mediaContent,
        ];
        return $this->getCandyStoryRepo()->insertAndGetModel($storyModel);
    }

    /**
     * @param $strFile
     * @param $saveYear
     * @param $saveMonth
     * @param $saveDay
     * @param int $sort
     * @return mixed|string
     * @throws \Laravelladder\Core\Exceptions\Endpoints\ArgumentRequiredException
     * @throws \Laravelladder\Core\Exceptions\Validations\ValidationException
     */
    public function uploadImgToAliyun($strFile,$saveYear,$saveMonth,$saveDay,$sort=0){
        $fileName = time().rand(100,9999).rand(500,999). '.jpg';
        $filePath = "/tmp/".$fileName;
        \Log::debug("图片_sort值{$sort}_计算sort值_{$fileName}");
        file_put_contents($filePath,base64_decode($strFile));
        $objName     = UploadOssObject::getInstance()->setArguments([
            UploadOssObject::ARGUMENT_FILE_NAME  => $fileName,
            UploadOssObject::ARGUMENT_FILE_PATH  => $filePath,
            UploadOssObject::ARGUMENT_SAVE_YEAR  => $saveYear,
            UploadOssObject::ARGUMENT_SAVE_MONTH => $saveMonth,
            UploadOssObject::ARGUMENT_SAVE_DAY   => $saveDay,
        ])->dryRun();
        unlink($filePath);
        return $objName;
    }
}